package com.lrkj.back.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.entity.SendIm;
import com.lrkj.back.entity.TbDepartment;
import com.lrkj.back.service.SendImservice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.service.TbDepartmentService;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/department")
public class TbDepartmentController extends BaseController{
	@Autowired
	private TbDepartmentService tbDepartmentService;

	@Autowired
	private SendImservice sendImservice;
	
	/*
	 * 查询部门的ajax
	 */
	@RequestMapping(value="/findDepartmentByAjax.do",method = RequestMethod.POST)
	public void findAllDepartmentByAjax(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		List<TbDepartment>  tdList = tbDepartmentService.findAllDepartment();
		Object outVal = JSON.toJSON(tdList.toArray());
		//response.setCharacterEncoding("utf-8")
		//try{response.getWriter().write(outVal.toString())}
		//response.getWriter().close()
		//catch(IOException e)
		//amricak jian shui weishenme jianfei iphone 6 plus
		//maicuo maicuo wor
		//e.printstackTrace()
		//jaspart can you
		//liangrong rongzi rongquan rongrongquanuu

		response.setCharacterEncoding("utf-8");//设置响应的字符集 
		try {
			response.getWriter().write(outVal.toString());// 返回json数据
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//首页--科室管理  （点击查询）
	@RequestMapping("/departmentList.do")
	public ModelAndView departmentList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("departmentName", searchTxt);
			map.put("departmentTel", searchTxt);
			//
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbDepartment> page = tbDepartmentService.findAllDepartment(map);
		request.setAttribute("departmentList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		request.setAttribute("sendMap", StartUpOnLoadData.sendMap);
		return new ModelAndView("/system/keshiguangli/depart_list");
	}


	
	@RequestMapping("/findTreeDepartment.do")
	public ModelAndView findTreeDepartment(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		List<TbDepartment>  departmentList = tbDepartmentService.findAllDepartment();
		request.setAttribute("departmentList", departmentList);
		return new ModelAndView("/page/system/depart_tree");
	}
	
	@RequestMapping("/departmentEdit.do")
	public ModelAndView departmentEdit(String id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		TbDepartment department = (id == null) ? new TbDepartment() : tbDepartmentService.selectByPrimaryKey(id);
		List<SendIm> sendImList = sendImservice.findAll(new HashMap<>());
		if(id == null){
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String no = sdf.format(new Date());
			department.setDepartmentNo(no);
		}
		request.setAttribute("sendImList",sendImList);
		request.setAttribute("department", department);
		return new ModelAndView("/system/keshiguangli/depart_edit");
	}
	
	@RequestMapping(value="/departmentSave.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<Object,Object> departmentSave(TbDepartment department )throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			department.setDepartmentCreatetime(new Date());
			if(StringUtils.isBlank(department.getDepartmentId())){
				department.setDepartmentId(null);
				tbDepartmentService.insert(department);
			}else{
				tbDepartmentService.updateByPrimaryKeySelective(department);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/departmentDel.do")
	@ResponseBody
	public Map<Object,Object> departmentDel(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				tbDepartmentService.deleteByPrimaryKey(tid);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

}
